﻿using Newtonsoft.Json;
using Sunny.UI;
using System.Data;
using System.Windows.Forms;
using WinApp.i18n;
using WinApp.Model;
using WinApp.Model.AjaxResult;
using WinApp.Until;
using WinApp.ViewModel;

namespace WinApp.View
{
    public partial class CourseDetails : UIPage
    {
        public WinApp _parent { get; set; }
        public CourseDetails()
        {
            InitializeComponent();

            this.Text = Language.GetLang("CourseDetails");

            uiDataGridView1.ColumnHeadersVisible = false;
            uiDataGridView1.RowTemplate.Height = 65;
            uiDataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;

            //uiDataGridView1.DefaultCellStyle.SelectionBackColor = uiDataGridView1.DefaultCellStyle.BackColor;
            //uiDataGridView1.DefaultCellStyle.SelectionForeColor = uiDataGridView1.DefaultCellStyle.ForeColor;

            uiDataGridView1.RowsDefaultCellStyle.BackColor = Color.FromArgb(243, 249, 255);
            this.BackColor = Color.FromArgb(243, 249, 255);
            //uiDataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.White;
        }

        private void CourseDetails_Load(object sender, EventArgs e)
        {
            //QueryData();
            uiDataGridView1.ReadOnly = true;

        }

        public void QueryData()
        {

            LoadingHelper.ShowLoadingScreen();

            DataTable table = new DataTable();
            table.Columns.Add("Title");
            table.Columns.Add("Status");
            table.Columns.Add("StatusStr");
            table.Columns.Add("Play");
            table.Columns.Add("Level");
            table.Columns.Add("NodeType");

            table.Columns.Add("CourseUnitType");
            table.Columns.Add("LearningStatuTableId");
            table.Columns.Add("StructureId");
            table.Columns.Add("EnrollmentId");
            table.Columns.Add("CourseUnitId");
            table.Columns.Add("CourseUnitLanuchPath");
            table.Columns.Add("CourseId");
            table.Columns.Add("StructureName");

            List<MyStructureWithLearningStatuOutPutDto> MainList = new List<MyStructureWithLearningStatuOutPutDto>();

            var tagCourseSessionDto = this.Tag as TagCourseSessionDto;
            //ShowSuccessDialog("正确信息提示框");
            if (tagCourseSessionDto == null)
            {
                ShowWarningDialog(Language.GetLang("notFindTag"));
                return;
            }

            AjaxResult? result = CourseDetailsModel.GetCourseDetails(tagCourseSessionDto);

            var mainData = JsonConvert.DeserializeObject<MyStructureWithLearningStatuOutPutDto>(result.Data.ToString());

            //赋值 lable
            lbStructureName.Text = mainData.StructureName;
            lbCourseUnitCount.Text = mainData.CourseUnitCount.ToString();
            lbCumulativeLearningTime.Text = "| Learning " + UtilHelper.DoubleTimeToString(mainData.CumulativeLearningTime);
            var strCompletePercent = mainData.CompletePercent.IsNullOrEmpty() ? "0" : mainData.CompletePercent;

            double.TryParse(mainData.CompletePercent, out double dbCompletePercent);
            string percentage = dbCompletePercent.ToString("P0");
            Console.WriteLine(percentage);  // 输出： 3.00%
            lbCompletePercent.Text = "| Progress " + percentage;
            lbStrTime.Text = "| " + tagCourseSessionDto.StrStartTime + "--" + tagCourseSessionDto.StrEndTime;


            foreach (var item in mainData.Children)
            {
                string itemName = item.StructureName + Environment.NewLine + UtilHelper.DoubleTimeToString(item.CumulativeLearningTime);
                string itemStatus = UtilHelper.GetLearningStatusStr(item.LearningStatu);
                table.Rows.Add(itemName, item.LearningStatu, itemStatus, Language.GetLang("play"), 0, item.NodeType, item.CourseUnitType, item.LearningStatuTableId, item.StructureId, item.EnrollmentId, item.CourseUnitId, item.CourseUnitLanuchPath, item.CourseId, item.StructureName);
                //MainList.Add(item);

                foreach (var item1 in item.Children)
                {
                    string item1Name = item1.StructureName + Environment.NewLine + UtilHelper.DoubleTimeToString(item1.CumulativeLearningTime);
                    string item1Status = UtilHelper.GetLearningStatusStr(item1.LearningStatu);
                    //table.Rows.Add(item1Name, item1Status, Language.GetLang("play"));
                    table.Rows.Add(item1Name, item1.LearningStatu, item1Status, Language.GetLang("play"), 1, item1.NodeType, item.CourseUnitType, item.LearningStatuTableId, item.StructureId, item.EnrollmentId, item.CourseUnitId, item.CourseUnitLanuchPath, item.CourseId, item.StructureName);
                    //MainList.Add(item);

                    foreach (var item2 in item1.Children)
                    {
                        string item2Name = item2.StructureName + Environment.NewLine + UtilHelper.DoubleTimeToString(item2.CumulativeLearningTime);
                        string item2Status = UtilHelper.GetLearningStatusStr(item2.LearningStatu);
                        //table.Rows.Add(item2Name, item2Status, Language.GetLang("play"));
                        table.Rows.Add(item2Name, item2.LearningStatu, item2Status, Language.GetLang("play"), 2, item2.NodeType, item.CourseUnitType, item.LearningStatuTableId, item.StructureId, item.EnrollmentId, item.CourseUnitId, item.CourseUnitLanuchPath, item.CourseId, item.StructureName);
                        //MainList.Add(item2);

                        foreach (var item3 in item2.Children)
                        {
                            string item3Name = item3.StructureName + Environment.NewLine + UtilHelper.DoubleTimeToString(item3.CumulativeLearningTime);
                            string item3Status = UtilHelper.GetLearningStatusStr(item3.LearningStatu);
                            //table.Rows.Add(item3Name, item3Status, Language.GetLang("play"));
                            table.Rows.Add(item3Name, item3.LearningStatu, item3Status, Language.GetLang("play"), 3, item3.NodeType, item.CourseUnitType, item.LearningStatuTableId, item.StructureId, item.EnrollmentId, item.CourseUnitId, item.CourseUnitLanuchPath, item.CourseId, item.StructureName);
                            //MainList.Add(item3);
                        }
                    }
                }
            }



            //string s = "Fight Controls" + Environment.NewLine + Environment.NewLine + "00:00:00";

            //table.Rows.Add(s, "Not started", "Play");
            //table.Rows.Add(s, "Completed", "Play");
            //table.Rows.Add(s, "Not started", "Play");
            //table.Rows.Add(s, "Completed", "Play");
            //table.Rows.Add(s, "Not started", "Play");

            this.uiDataGridView1.DataSource = table;

            LoadingHelper.CloseForm();
            uiDataGridView1.ClearSelection();
        }



        private void uiDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (uiDataGridView1.Columns[e.ColumnIndex] is DataGridViewImageColumn && e.RowIndex >= 0)
            {
                // 获取当前行的数据行
                var currentRow = uiDataGridView1.Rows[e.RowIndex];
                if (currentRow.Cells["clNodeType"].Value is string && (string)currentRow.Cells["clNodeType"].Value != "1")
                {
                    CbtPlayInputDto dto = new CbtPlayInputDto();
                    var strStructureId = (string)currentRow.Cells["clStructureId"].Value;
                    int.TryParse(strStructureId, out int StructureId);
                    dto.StructureId = StructureId;

                    var strCourseId = (string)currentRow.Cells["clCourseId"].Value;
                    int.TryParse(strCourseId, out int CourseId);
                    dto.CourseId = CourseId;

                    var strEnrollmentId = (string)currentRow.Cells["clEnrollmentId"].Value;
                    int.TryParse(strEnrollmentId, out int EnrollmentId);
                    dto.EnrollmentId = EnrollmentId;

                    var strCourseUnitId = (string)currentRow.Cells["clCourseUnitId"].Value;
                    int.TryParse(strCourseUnitId, out int CourseUnitId);
                    dto.CourseUnitId = CourseUnitId;

                    var strCourseUnitLanuchPath = (string)currentRow.Cells["clCourseUnitLanuchPath"].Value;
                    dto.CourseUnitLanuchPath = strCourseUnitLanuchPath;

                    var strCourseUnitType = (string)currentRow.Cells["clCourseUnitType"].Value;
                    int.TryParse(strCourseUnitType, out int CourseUnitType);
                    dto.CourseUnitType = CourseUnitType;

                    var strLearningStatuTableId = (string)currentRow.Cells["clLearningStatuTableId"].Value;
                    int.TryParse(strLearningStatuTableId, out int LearningStatuTableId);
                    dto.LearningStatuTableId = LearningStatuTableId;

                    var strStructureName = (string)currentRow.Cells["clStructureName"].Value;
                    dto.StructureName = strStructureName;

                    _parent.OpenCbtPlay(dto);
                }
            }
        }

        private void uiDataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridViewRow row = uiDataGridView1.Rows[e.RowIndex];

            if (uiDataGridView1.Columns[e.ColumnIndex].Name == "clOp")
            {

                if (row.Cells["clNodeType"].Value is string && (string)row.Cells["clNodeType"].Value == "1")
                {
                    // 创建一个1x1像素的透明图像
                    Bitmap transparentImage = new Bitmap(1, 1);
                    transparentImage.SetPixel(0, 0, Color.Transparent);

                    row.Cells["clOp"].Value = transparentImage;
                    row.Cells["clOp"].ReadOnly = true;
                    row.Cells["clPlay"].Value = string.Empty;
                }
                else
                {
                    row.Cells["clOp"].Value = FileHelper.loadImageFromLocalPath(@"FileFolder/Icon/play.png");
                    row.Cells["clPlay"].Value = Language.GetLang("play");
                }

                if (row.Cells["clLevel"].Value is string)
                {
                    string strLevel = (string)row.Cells["clLevel"].Value;
                    if (strLevel == "0")
                    {
                        row.Cells["clTitle"].Style.Padding = new Padding(15, 0, 0, 0);
                    }
                    else if (strLevel == "1")
                    {
                        row.Cells["clTitle"].Style.Padding = new Padding(45, 0, 0, 0);
                    }
                    else if (strLevel == "2")
                    {
                        row.Cells["clTitle"].Style.Padding = new Padding(75, 0, 0, 0);
                    }
                    else if (strLevel == "3")
                    {
                        row.Cells["clTitle"].Style.Padding = new Padding(105, 0, 0, 0);
                    }
                }

                if (row.Cells["clStatusKey"].Value is string)
                {
                    string statusKey = (string)row.Cells["clStatusKey"].Value;
                    if (statusKey == "1")
                    {
                        row.Cells["clStatus"].Style.ForeColor = Color.Orange;
                    }
                    else if (statusKey == "2")
                    {
                        row.Cells["clStatus"].Style.ForeColor = Color.Green;
                    }
                }
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            _parent.SetNavMenulPageIndex(NavPageIndex.course, "");
        }

        private void uiDataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            return;
            if (e.ColumnIndex == 0 && e.RowIndex >= 0) // 在这里指定你想要自定义的列
            {
                e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground);

                string firstPart = "Hello"; // 第一部分的文本
                string secondPart = "World"; // 第二部分的文本

                using (Brush gridBrush = new SolidBrush(this.uiDataGridView1.GridColor),
                    backColorBrush = new SolidBrush(e.CellStyle.BackColor),
                    firstBrush = new SolidBrush(Color.Red), // 第一部分文本的颜色
                    secondBrush = new SolidBrush(Color.Blue)) // 第二部分文本的颜色
                {
                    using (Pen gridLinePen = new Pen(gridBrush))
                    {
                        // 绘制文本
                        e.Graphics.DrawString(firstPart, e.CellStyle.Font, firstBrush, e.CellBounds.X + 2, e.CellBounds.Y + 2);
                        Size firstPartSize = TextRenderer.MeasureText(firstPart, e.CellStyle.Font);
                        e.Graphics.DrawString(secondPart, e.CellStyle.Font, secondBrush, e.CellBounds.X + 2 + firstPartSize.Width, e.CellBounds.Y + 2);

                        e.Handled = true;
                    }
                }
            }
        }

        private void uiDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            uiDataGridView1.ClearSelection();
        }
    }
}
